layui.use(['table', 'HttpRequest', 'func'], function () {

    var $ = layui.$;
    var table = layui.table;
    var HttpRequest = layui.HttpRequest;
    var func = layui.func;
    /**
     * 管理
     */
    var WechatConfig = {
        tableId: "wechatConfigTable"
    };

    /**
     * 初始化表格的列
     */
    WechatConfig.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: ''},
            {field: 'wechatCode', sort: true, title: '微信公众号编码'},
            {field: 'appid', sort: true, title: '开发者ID(AppID)'},
            {field: 'token', sort: true, title: 'token值'},
            {field: 'wechatId', sort: true, title: '微信公众号ID'},
            {field: 'wechatNum', sort: true, title: '微信公众号'},
            {field: 'wechatName', sort: true, title: '公众号名称'},
            {field: 'isVerify', sort: true, title: '资质认证(0未认证,1认证)'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    WechatConfig.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(WechatConfig.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    WechatConfig.openAddDlg = function () {
        func.open({
            title: '添加',
            content: Feng.ctxPath + '/view/wechatConfig/add',
            tableId: WechatConfig.tableId
        });
    };

    /**
    * 点击编辑
    *
    * @param data 点击按钮时候的行数据
    */
    WechatConfig.openEditDlg = function (data) {
        func.open({
            title: '修改',
            content: Feng.ctxPath + '/view/wechatConfig/edit?id=' + data.id,
            tableId: WechatConfig.tableId
        });
    };

    /**
     * 导出excel按钮
     */
    WechatConfig.exportExcel = function () {
        var checkRows = table.checkStatus(WechatConfig.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    WechatConfig.onDeleteItem = function (data) {
        var operation = function () {
            var request = new HttpRequest(Feng.ctxPath + "/wechatConfig/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(WechatConfig.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            request.set("id", data.id);
            request.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + WechatConfig.tableId,
        url: Feng.ctxPath + '/wechatConfig/findPage',
        page: true,
        cellMinWidth: 100,
        cols: WechatConfig.initColumn(),
        request: {pageName: 'pageNo', limitName: 'pageSize'},
        parseData: Feng.parseData,
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        WechatConfig.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        WechatConfig.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        WechatConfig.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + WechatConfig.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            WechatConfig.openEditDlg(data);
        } else if (layEvent === 'delete') {
            WechatConfig.onDeleteItem(data);
        }
    });
});
